package cn.iocoder.yudao.module.trade.service.order.handler;

import cn.iocoder.yudao.framework.common.enums.trade.TradeOrderStatusEnum;
import cn.iocoder.yudao.framework.common.enums.trade.TransactionTypeEnum;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.module.member.api.config.dto.MemberConfMaterialDTO;
import cn.iocoder.yudao.module.member.api.config.dto.MemberConfRespDTO;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
import cn.iocoder.yudao.module.pay.enums.notify.PayNotifyTypeEnum;
import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeOrderOrderReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryTypeEnum;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.ORDER_CREATE_FAIL_USER_NO_PERMISSION;

/**
 * 实物金属交易模式处理器
 * @author FFFF
 */
@Component
public class FullPayBuyTransactionHandler extends AbstractTransactionModeHandler {

    private static final TransactionTypeEnum TRANSACTION_TYPE = TransactionTypeEnum.FULL_PAY_BUY;
    
    @Override
    public Integer getTransactionType() {
        return TRANSACTION_TYPE.getType();
    }
    
    @Override
    public void validatePermission(MemberConfRespDTO memberConf) {
        if (Boolean.FALSE.equals(memberConf.getFullPayBuy())) {
            throw ServiceExceptionUtil.exception(ORDER_CREATE_FAIL_USER_NO_PERMISSION, TRANSACTION_TYPE.getDesc());
        }
    }
    
    @Override
    public void validateTradeLimits(MemberConfMaterialDTO materialConf, AppTradeOrderOrderReqVO settlementReqVO) {
        // 调用父类的通用限制校验
        super.validateTradeLimits(materialConf, settlementReqVO);
    }

    @Override
    protected void setPriceResult(TradePriceCalculateReqBO calculateReqBO, TradePriceCalculateRespBO result, TradePriceCalculateRespBO context) {
//        result.setTotalPrice(context.getMetalPrice());
        result.setPayType(PayNotifyTypeEnum.SETTLE_ORDER.getType());
    }


    /**
     * 计算支付价格
     */
    @Override
    protected BigDecimal calculatePayPrice(TradePriceCalculateReqBO calculateReqBO, TradePriceCalculateRespBO result) {
        return result.getMetalPrice();
    }

    /*
     * 全款模式:只有付一次钱 (快递另外考虑)
     * 只有一个结算单
     */
    @Override
    public TradeOrderStatusEnum getNextStatusBySuceess(TradeOrderDO order, Integer payType, Boolean isStore) {
        TradeOrderStatusEnum nowStatus = TradeOrderStatusEnum.getEnum(order.getStatus());
        switch (nowStatus) {
            case WAIT_PAY_CHECK -> {
                if (PayNotifyTypeEnum.SETTLE_ORDER.getType().equals(payType)) {
                    if (isStore) {
                        return TradeOrderStatusEnum.COMPLETED;
                    }
                    return TradeOrderStatusEnum.PREPARE;
                }
                return TradeOrderStatusEnum.UNPAID;
            }
            case PREPARE -> {
                if (DeliveryTypeEnum.PICK_UP.getType().equals(order.getDeliveryType())) {
                    return TradeOrderStatusEnum.PREPARE_DONE;
                }
                return TradeOrderStatusEnum.DELIVERED;
            }
            case DELIVERED -> {
                return TradeOrderStatusEnum.COMPLETED;
            }
            default -> {
                return null;
            }
        }
    }


    @Override
    public PayOrderCreateReqDTO creatSettlePay(TradeOrderDO order, Boolean isViolate) {
        return null;
    }
} 